﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Threading;
using System.Windows.Media;
using System.Windows.Media.Imaging;

namespace Microsoft.Forums.ForumsClient.StatusBar
{
    public class StatusBarViewModel : BaseDataModel
    {
        /// <summary>
        /// Application Connected Icon (Network alive & forum service up & Logged in)
        /// </summary>
        BitmapImage connectedIcon;

        /// <summary>
        /// Application disconnected icon
        /// </summary>
        BitmapImage disconnectedIcon;

        /// <summary>
        ///  Application connection state icon that is finally displayed on the status bar
        /// </summary>
        BitmapImage currentNetworkIcon;

        /// <summary>
        ///  Status bar needs to know whenever there is change in network status so hold reference to that.
        /// </summary>
        NetworkConnection network;

        /// <summary>
        ///  Status bar needs to know the current user information / state.
        /// </summary>
        UserLogonInformation currentUser;

        /// <summary>
        ///  Network Tooltip text.
        /// </summary>
        string networkToolTipText;

        /// <summary>
        /// Tooltip property
        /// </summary>
        public string NetworkToolTip
        {
            get
            {
                return networkToolTipText;
            }

            private set
            {
                networkToolTipText = value;
                OnPropertyChanged("NetworkToolTip");
            }
        }

        /// <summary>
        ///  Get / Set Network Icon displayed on the Status Bar.
        /// </summary>
        public BitmapImage NetworkIcon
        {
            get
            {
                return currentNetworkIcon;
            }
            private set
            {
                currentNetworkIcon = value;
                OnPropertyChanged("NetworkIcon");
            }
        }

        /// <summary>
        ///  Constructor of Status Bar Data Model
        /// </summary>
        /// <param name="appDataModel">Application Data Model</param>
        public StatusBarViewModel(AppDataModel appDataModel)
        {
            // Load online / offline icons. Freeze in order to avoid issues in 3.5
            connectedIcon = new BitmapImage(new Uri("pack://application:,,,/images/Online.ico", UriKind.RelativeOrAbsolute));
            if (connectedIcon.CanFreeze)
            {
                connectedIcon.Freeze();
            }

            disconnectedIcon = new BitmapImage(new Uri("pack://application:,,,/images/Offline.ico", UriKind.RelativeOrAbsolute));

            if (disconnectedIcon.CanFreeze)
            {
                disconnectedIcon.Freeze();
            }

            // Hold reference to data that this sub data model is interested in.
            network = appDataModel.Network;
            currentUser = appDataModel.CurrentUser;

            currentNetworkIcon = disconnectedIcon;

            if (currentUser.LogOnState == UserLogonInformation.LogOnStatus.Authenticated)
            {
                NetworkIcon = connectedIcon;
                NetworkToolTip = Properties.Resources.LoggedInContent;
            }
            else
            {
                NetworkToolTip = Properties.Resources.NotLoggedContent;
            }

            network.PropertyChanged += new System.ComponentModel.PropertyChangedEventHandler(Network_PropertyChanged);
            currentUser.PropertyChanged += new System.ComponentModel.PropertyChangedEventHandler(CurrentUser_PropertyChanged);
        }

        /// <summary>
        ///  This is raised for any property change notification from CurrentUser 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        void CurrentUser_PropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e)
        {
            if (e.PropertyName.Equals("LogOnState"))
            {
               if (currentUser.LogOnState == UserLogonInformation.LogOnStatus.Authenticated)
               {
                   NetworkIcon = connectedIcon;
                   NetworkToolTip = Properties.Resources.LoggedInContent;
               }
               else if (currentUser.LogOnState == UserLogonInformation.LogOnStatus.Authenticated)
               {
                   NetworkIcon = disconnectedIcon;
                   NetworkToolTip = Properties.Resources.NotLoggedContent;
               }
            }            
        }

        void Network_PropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e)
        {
            if (network.IsConnected == false)
            {
                NetworkIcon = disconnectedIcon;
                NetworkToolTip = Properties.Resources.NotConnectedContent;
            }
            else
            {
                NetworkIcon = connectedIcon;
            }   
        }
    }
}
